from tkinter import *
from config import WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, apply_dpi_scaling
from text_manager import TextManager
from section_manager import SectionManager
from signal_controller import SignalController
from switch_controller import SwitchController
from station_controller import StationController
from ui_components import UIComponents

class RailwayInterlockingSystem:
    def __init__(self):
        # 创建主窗口
        self.root = Tk()
        self.root.title(WINDOW_TITLE)
        self.root.geometry(f"{WINDOW_WIDTH}x{WINDOW_HEIGHT}+200+200")
        
        # 创建画布
        self.canvas = Canvas(self.root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg="white")
        self.canvas.pack()
        
        # 创建文本框和滚动条
        self._create_text_area()
        
        # 创建文本管理器
        self.text_manager = TextManager(self.text_box, self.root)
        
        # 创建各个控制器
        self.section_manager = SectionManager(self.canvas, self.text_manager)
        self.signal_controller = SignalController(self.canvas, self.text_manager, self.section_manager)
        self.switch_controller = SwitchController(self.canvas, self.text_manager, self.section_manager)
        self.station_controller = StationController(self.canvas, self.text_manager)
        
        # 创建UI组件
        self.ui_components = UIComponents(self.root, self.canvas)
        
        # 创建控制按钮
        self._create_control_buttons()
        
        # 应用DPI缩放
        apply_dpi_scaling(self.root)
        
        # 创建更新消息的标签
        self.update_label = Label(self.root, text="", font=("Helvetica", 12), fg="black")
        self.update_label.pack()
    
    def _create_text_area(self):
        """创建文本区域"""
        # 创建文本框
        self.text_box = Text(self.canvas, wrap=WORD, width=50, height=12, fg="black")
        self.canvas.create_window(1180, 480, window=self.text_box, anchor='nw')
        
        # 创建滚动条
        self.scrollbar = Scrollbar(self.root, command=self.text_box.yview)
        self.scrollbar.pack(side=RIGHT, fill=Y)
        self.text_box.config(yscrollcommand=self.scrollbar.set)
    
    def _create_control_buttons(self):
        """创建控制按钮"""
        # 道岔控制按钮
        self._create_switch_buttons()
        
        # 区段控制按钮
        self._create_section_buttons()
        
        # 站台控制按钮
        self._create_station_buttons()
        
        # 信号按钮
        self._create_signal_buttons()
    
    def _create_switch_buttons(self):
        """创建道岔控制按钮"""
        Button(self.root, text="设置定位", fg="white", 
               command=self._set_switch_normal, bd='2', bg='blue').place(width=70, height=40, x=1300, y=30)
        Button(self.root, text="设置反位", fg="white", 
               command=self._set_switch_reverse, bd='2', bg='blue').place(width=70, height=40, x=1410, y=30)
        Button(self.root, text="取消进路建立", fg="white", 
               command=self._cancel_route, bd='2', bg='blue').place(width=100, height=40, x=1170, y=30)
    
    def _create_section_buttons(self):
        """创建区段控制按钮"""
        Button(self.root, text="设置区段占用", fg="white", 
               command=lambda: self.section_manager.enter_selection_mode("set"), 
               bd='2', bg='blue').place(width=100, height=40, x=100, y=520)
        Button(self.root, text="取消区段占用", fg="white", 
               command=lambda: self.section_manager.enter_selection_mode("cancel"), 
               bd='2', bg='blue').place(width=100, height=40, x=230, y=520)
        Button(self.root, text="取消选择模式", fg="white", 
               command=self.section_manager.exit_selection_mode, 
               bd='2', bg='red').place(width=100, height=40, x=360, y=520)
    
    def _create_station_buttons(self):
        """创建站台控制按钮"""
        Button(self.root, text="设置站台门紧急关闭状态", fg="white", 
               command=self.station_controller.set_emergency_state, 
               bd='2', bg='blue').place(width=180, height=40, x=490, y=520)
        Button(self.root, text="取消站台门紧急关闭状态设置", fg="white", 
               command=self.station_controller.cancel_emergency_state, 
               bd='2', bg='blue').place(width=200, height=40, x=700, y=520)
        Button(self.root, text="站台门打开", fg="white", 
               command=self.station_controller.open_station_door, 
               bd='2', bg='blue').place(width=100, height=40, x=930, y=520)
        Button(self.root, text="站台门关闭", fg="white", 
               command=self.station_controller.close_station_door, 
               bd='2', bg='blue').place(width=100, height=40, x=1060, y=520)
    
    def _create_signal_buttons(self):
        """创建信号按钮"""
        signal_buttons = [
            ("XC", 905, 108), ("F4", 375, 108), ("F8", 1455, 108),
            ("F5", 450, 318), ("F2", 355, 257), ("F6", 825, 408),
            ("F1", 90, 468), ("F3", 460, 468), ("SC", 1070, 468)
        ]
        
        for text, x, y in signal_buttons:
            Button(self.root, text=text, fg="white", 
                   command=lambda t=text: self.signal_controller.on_signal_click(t), 
                   bd='2', bg='blue').place(width=25, height=25, x=x, y=y)
    
    def _set_switch_normal(self):
        """设置道岔定位"""
        # 这里可以添加道岔定位的逻辑
        pass
    
    def _set_switch_reverse(self):
        """设置道岔反位"""
        # 这里可以添加道岔反位的逻辑
        pass
    
    def _cancel_route(self):
        """取消进路建立"""
        self.canvas.delete("route_green")
        text_to_insert = ["已取消建立进路"]
        self.text_manager.insert_text_with_delay(text_to_insert)
    
    def _on_signal_click(self, signal_name):
        """信号点击事件"""
        # 这里可以添加信号点击的逻辑
        text_to_insert = [f"点击了信号: {signal_name}"]
        self.text_manager.insert_text_with_delay(text_to_insert)
    
    def run(self):
        """运行程序"""
        self.root.mainloop()

if __name__ == "__main__":
    app = RailwayInterlockingSystem()
    app.run() 